#pragma once

#include <Network/TcpConnection.h>
#include <Game/ServerPacketProducer.h>

#include "User.h"
#include "Grid.h"

class Room
{
public:
	Room(ServerPacketProducer& packetProducer, Grid& grid, size_t maxUser=4, bool vs2=true);
	~Room(void);

	void Enter(uint64_t userId, TcpConnection* connection)
	{
		uint8_t teamId = (vs2_)? members_.size()&1 : members_.size();

		members_.insert(concurrent_unordered_map<uint64_t, User*>::value_type(userId, new User(userId, teamId, connection)));
	}

	void Leave(uint64_t userId)
	{
		reader_writer_lock::scoped_lock scoped(lock_members_);
		members_.unsafe_erase(userId);
	}

	void Advance(uint64_t userId, int x, int y)
	{
		concurrent_unordered_map<uint64_t, User*>::iterator itr = members_.find(userId);
		if(members_.end() == itr || (*itr).second == NULL)
			return;

		User* user = (*itr).second;
		grid_.Mark(user->GetTeamId(), x, y);
	}

	void BroadCast()
	{
		concurrent_unordered_map<uint64_t, User*>::iterator itr = members_.begin();
		for(;itr != members_.end(); ++itr)
		{
			User* user = (*itr).second;
			TcpConnection* conn = user->GetConnection();
			packetProducer_->
		}
	}

private:
	size_t	maxUser_;
	bool	vs2_;

	Grid&	grid_;

	concurrent_unordered_map<uint64_t, User*> members_, out_, team_;
	reader_writer_lock lock_members_;

	ServerPacketProducer& packetProducer_;
};
